package com.hrm.controller.org;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;
import org.springframework.web.servlet.mvc.AbstractController;

import com.hrm.model.dto.org.OrgBaseDTO;
import com.hrm.model.service.OrgService;

public class OrgTreeController extends AbstractController{
	
	private OrgService orgService;

	public void setOrgService(OrgService orgService) {
		this.orgService = orgService;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		String tree = arg0.getParameter("tree");
		tree = (tree==null)? "1":tree;
		String orgcode = arg0.getParameter("code");
		orgcode = (orgcode==null)? "01000000":orgcode;
		String sdate = arg0.getParameter("sdate");
		if(sdate ==null || sdate.equals("")){
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH)+1;
			int date = cal.get(Calendar.DATE);
			sdate = (year+"-"+month+"-"+date);
		}else if(sdate.length()>=10){
			sdate=sdate.substring(0,10);
		}
		OrgBaseDTO dto = orgService.getOrgbase(orgcode);
		
		System.out.println("time "+sdate);
		
		String orgtree = orgService.getMenuTreeString(sdate).toString();
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("orgbaseinfo", dto);
		mv.addObject("orgtree",orgtree);
		mv.addObject("sdate", sdate);
		mv.setViewName("org_tree_main"+tree);
		if(tree.equals("1")){
			return new ModelAndView("redirect:/orgBaseInfoController.cs?sdate="+sdate);
		}else if(tree.equals("2")){
			return new ModelAndView("redirect:/orgPersoninfoController.cs?sdate="+sdate);			
		}else if(tree.equals("3")){
			return new ModelAndView("redirect:/orgMemberCountController.cs");			
		}
		
		return mv;
	}
}
